﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace HuiLian.Common.Helpers
{
    public class ExtendHelper
    {
        public static List<T> GetList<T>(DataTable table)
        {
            List<T> list = new List<T>();
            T t = default(T);
            PropertyInfo[] propertypes = null;
            string tempName = string.Empty;
            foreach (DataRow row in table.Rows)
            {
                t = Activator.CreateInstance<T>();
                propertypes = t.GetType().GetProperties();
                foreach (PropertyInfo pro in propertypes)
                {
                    tempName = pro.Name;
                    if (table.Columns.Contains(tempName))
                    {
                        object value = row[tempName].ToString().Trim();
                        if (!value.ToString().Equals(""))
                        {
                            switch (tempName)
                            {
                                case "普查时间":
                                    DateTime date = Convert.ToDateTime(value);
                                    pro.SetValue(t, date, null);
                                    break;
                                default:
                                    pro.SetValue(t, Convert.ChangeType(value, pro.PropertyType), null);
                                    break;
                            }
                        }
                    }
                }
                list.Add(t);
            }
            return list.Count == 0 ? null : list;
        }
    }
}
